package com.zyf.dao.daoImpl;

import com.zyf.dao.dao;
import com.zyf.entity.book;
import com.zyf.util.DButil;

import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class daoImpl implements dao {


    public List<book> findBook(Map<String, String> bookinfo) {
        String key = "";
        String value = "";

        //一堆初始化
        Connection conn = null;
        PreparedStatement psmt = null;
        int n = 0;
        ResultSet rs = null;
        String sql = null;

        //当bookinfo为null时为查询所有
        if (bookinfo.isEmpty()) {
            sql = "select * from library";
        } else {
            //通过Map储存键名和条件
            Iterator<String> it = bookinfo.keySet().iterator();
            key = it.next();
            value = bookinfo.get(key);
            sql = "select * from library where " + key + " like ?";
        }


        List<book> bkrs = new ArrayList<book>();


        try {
            conn = DButil.getConnetion();
            psmt = conn.prepareStatement(sql);
            if (!bookinfo.isEmpty()) {
                psmt.setString(1, "%" + value + "%");
            }
            rs = psmt.executeQuery();

            while (rs.next()) {
                book rb = new book();
                rb.setBookId(rs.getInt("bookid"));
                rb.setBookName(rs.getString("bookname"));
                rb.setBookAuther(rs.getString("bookauther"));
                rb.setEnterDate(rs.getDate("enterdate"));
                rb.setPrice(rs.getFloat("price"));
                rb.setBookLocation(rs.getString("booklocation"));
                bkrs.add(rb);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.closeAll(rs, psmt, conn);
        }
        return bkrs;

    }

    @Override
    public book findById(int id) {
        Connection conn = null;
        PreparedStatement psmt = null;
        String sql = "select * from library where bookid = ?";
        ResultSet rs = null;
        book rb = null;
        try {
            conn = DButil.getConnetion();
            psmt = conn.prepareStatement(sql);
            psmt.setInt(1, id);
            rs = psmt.executeQuery();

            while (rs.next()) {
                rb = new book();
                rb.setBookId(rs.getInt("bookid"));
                rb.setBookName(rs.getString("bookname"));
                rb.setBookAuther(rs.getString("bookauther"));
                rb.setEnterDate(rs.getDate("enterdate"));
                rb.setPrice(rs.getFloat("price"));
                rb.setBookLocation(rs.getString("booklocation"));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.closeAll(rs, psmt, conn);
        }

        return rb;
    }

    @Override
    public int insertBook(book abookWithoutId) {
        Connection conn = null;
        PreparedStatement psmt = null;
        int n = -1;

        String bookName = abookWithoutId.getBookName();
        String auther = abookWithoutId.getBookAuther();
        String location = abookWithoutId.getBookLocation();
        float price = abookWithoutId.getPrice();
        java.sql.Date sqlDate = abookWithoutId.getEnterDate();

        try {
            conn = DButil.getConnetion();
            String sql = "insert into library (bookname,bookauther,price,enterdate,booklocation) values (?,?,?,?,?)";
            psmt = conn.prepareStatement(sql);
            psmt.setString(1, bookName);
            psmt.setString(2, auther);
            psmt.setFloat(3, price);
            psmt.setDate(4, sqlDate);
            psmt.setString(5, location);
            n = psmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return n;
    }

    @Override
    public int deleteBook(int id) {
        Connection conn = null;
        PreparedStatement psmt = null;
        int n = -1;

        try {
            conn = DButil.getConnetion();
            String sql = "delete from library where bookid = ?";
            psmt = conn.prepareStatement(sql);
            psmt.setInt(1, id);
            n = psmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return n;
    }

    @Override
    public int setProperties(int id, Map<String, String> settings) {
        String key = "";
        String value = "";
        int n = -1;

        Iterator<String> it = settings.keySet().iterator();
        while (it.hasNext()) {
            key = it.next();
            value = settings.get(key);
        }

        Connection conn = null;
        PreparedStatement psmt = null;
        String sql = "update library set " + key + " = ? where bookid = ?";
        try {
            conn = DButil.getConnetion();
            psmt = conn.prepareStatement(sql);
            psmt.setString(1, value);
            psmt.setInt(2, id);
            n = psmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return n;
    }

    @Override
    public int refreshEnterDate(int id, Date sqlDate) {
        Connection conn = null;
        PreparedStatement psmt = null;
        int n = -1;

        try {
            conn = DButil.getConnetion();
            String sql = "update library set enterdate = ? where bookid = ?";
            psmt = conn.prepareStatement(sql);
            psmt.setDate(1, sqlDate);
            psmt.setInt(2, id);
            n = psmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return n;
    }

}
